library(tidyverse)
library(haven)

type = read_stata("cnty_analysis.dta") %>%
  group_by(fips) %>%
  summarise(boxes_combo = sum(boxes_combo),
            boxes_dairy = sum(boxes_dairy),
            boxes_milk = sum(boxes_milk),
            boxes_fruitveg = sum(boxes_fruitveg),
            boxes_meat = sum(boxes_meat)) %>%
  pivot_longer(cols = c("boxes_combo", "boxes_dairy", 
                        "boxes_milk", "boxes_fruitveg",
                        "boxes_meat"),
               names_to = "type", values_to = "boxes") %>%
  ungroup() %>%
  group_by(type) %>%
  summarise(boxes = sum(boxes)) %>%
  mutate(pct = boxes/sum(boxes)*100)

## Plot
food_type = ggplot(type, aes(x = reorder(type, -pct), y = pct)) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(aes(label=
                  paste0(I(round(pct, digits = 2)), "%")), 
            position=position_dodge(width=0.9), vjust=-0.45, size = 2) +
  labs(x = "Food Box Type", y = "Share of Boxes") +
  scale_y_continuous(limits = c(0, 40), breaks = c(0, 10, 20, 30, 40),
                     labels = function(x) paste0(x, "%")) +
  scale_x_discrete(labels = c("Fruit/Veg.", "Combo",
                              "Milk", "Dairy", "Meat")) +
  theme_bw() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(colour = "black"),
        legend.position = "bottom")







